describe('分块上传相关操作', function () {
    var key = 'demo.txt';
    var file = blob({ size: 1024 * 1024 * 15 });
    var uploadId = '';
    var sliceList = [];
    var uploadPartList = [];
    describe('分块上传文件', function () {
        it('初始化分块上传', function (done) {
            this.timeout(2000);
            ks3.initiateMultipartUpload({
                key,
                file,
            }).then(res => {
                uploadId = data.body.InitiateMultipartUploadResult.UploadId;
                should.not.exist(res.error);
                data.statusCode.should.be.equal(200);
                done();
            })
        })
        it('给文件分块', function (done) {
            this.timeout(2000);
            // 获取文件块数
            let totalSize = file.size;
            let chunkSize = ks3.config.chunkSize;
            let chunkNum = Math.ceil(totalSize / chunkSize);
            // 上传的分片数据
            for (let i = 1; i <= chunkNum; i++) {
                // 给文件分块
                let endSize = i == chunkNum ? totalSize : chunkSize * i;
                fileSlice(file, chunkSize * (i - 1), endSize, function (result) {
                    sliceList.push({
                        key: key,
                        partNumber: i,
                        body: result,
                        uploadId: uploadId,
                        uploadType: 'upload',
                    })
                    if (sliceList.length === chunkNum) {
                        done()
                    }
                });
            };
        })
        it('上传文件块', function (done) {
            this.timeout(20000);
            var num = 0;
            sliceList.forEach(item => {
                ks3.uploadPart(item).then(res => {
                    uploadPartList.push({
                        partNumber: item.partNumber,
                        etag: data.headers.etag
                    });
                    should.not.exist(res.error);
                    data.statusCode.should.be.equal(200);
                    num++;
                    console.log('已上传' + num + '块,一共有' + sliceList.length + '块')
                    if (num === sliceList.length) {
                        done();
                    }
                })
            })

        })
        it('获取当前任务已上传的分块', function (done) {
            this.timeout(2000);
            ks3.listParts({
                key,
                uploadId
            }).then(res => {
                data.body['ns2:ListPartsResult']['ns2:Part'].length.should.be.equal(sliceList.length);
                should.not.exist(res.error);
                data.statusCode.should.be.equal(200);
                done();
            })
        })
        it('完成分块上传，合并分块', function (done) {
            this.timeout(2000);
            ks3.completeMultipart({
                key,
                uploadId,
                parts: uploadPartList
            }).then(res => {
                should.not.exist(res.error);
                data.statusCode.should.be.equal(200);
                done();
            })
        })
        it('获取文件信息', function (done) {
            this.timeout(2000);
            ks3.listObjects({
                prefix: key
            }).then(res => {
                let contents = data.body.ListBucketResult.Contents;
                let object;
                if (contents instanceof Array) {
                    object = contents.find((item) => item.Key === key);
                } else {
                    object = contents;
                }
                object.Size.should.be.equal(file.size.toString());
                should.not.exist(res.error);
                data.statusCode.should.be.equal(200);
                done();
            })
        })
        it('删除文件', function (done) {
            this.timeout(2000);
            ks3.delObject({
                key
            }).then(res => {
                should.not.exist(res.error);
                data.statusCode.should.be.equal(204);
                done();
            })
        })
    })
    describe('取消分块上传', function () {
        it('初始化分块上传', function (done) {
            this.timeout(2000);
            ks3.initiateMultipartUpload({
                key,
                file
            }).then(res => {
                uploadId = data.body.InitiateMultipartUploadResult.UploadId;
                should.not.exist(res.error);
                data.statusCode.should.be.equal(200);
                done();
            })
        })
        it('取消分块上传', function (done) {
            this.timeout(2000);
            ks3.abortMultipartUpload({
                key,
                uploadId
            }).then(res => {
                should.not.exist(res.error);
                data.statusCode.should.be.equal(204);
                done();
            })
        })
    })
})